package com.shiguang.utils

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.shiguang.utils.databinding.FragmentFirstBinding

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class FirstFragment : Fragment() {

    private var _binding: FragmentFirstBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentFirstBinding.inflate(inflater, container, false)
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 获取此前在Activity中设置的NavGraph中的参数
        val map = findNavController().graph.arguments
        val argument = map["arg"]
        if(null != argument) {
            val bundle = argument?.defaultValue as Bundle
            // 获取Bundle中的值
            val text = bundle.getString("text", "")
            binding.textviewFirst.text = text
        }
        Log.e("dushiguang" , "argument----$argument");

        val secondBundle = Bundle()
        secondBundle.putString("second", "传递到下一个Fragment的值")
        // 跳转至第二个Fragment页面，并且传递参数
        binding.buttonFirst.setOnClickListener {
            findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment, secondBundle)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}